/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.SPI.metadata;

import java.lang.annotation.Annotation;
import java.lang.reflect.AccessibleObject;
import java.util.Objects;

/**
 * @description: AbstractMetaData
 * @author: zzh
 * @date: 2021/11/1 13:24
 */
public abstract class AbstractMetaData implements MetaData {

    private Annotation[] annotations;

    private AccessibleObject accessibleObject;

    private String simpleName;

    private int modifiers;

    public AbstractMetaData(Annotation[] annotations, AccessibleObject accessibleObject, String simpleName, int modifiers) {
        this.annotations=annotations;
        this.accessibleObject = accessibleObject;
        this.simpleName = simpleName;
        this.modifiers = modifiers;
    }

    @Override
    public AccessibleObject getAccessibleObject() {
        return accessibleObject;
    }

    @Override
    public String getSimpleName() {
        return simpleName;
    }

    @Override
    public int getModifiers() {
        return modifiers;
    }

    @Override
    public Annotation[] getAnnotations() {
        return annotations;
    }

  @Override
  @SuppressWarnings("unchecked")
  public <A extends Annotation> A getAnnotation(Class<A> clazz) {
    if (Objects.isNull(clazz)) {
      for (Annotation annotation : getAnnotations()) {
        Class<? extends Annotation> annotationType = annotation.annotationType();
        if (annotationType.equals(clazz)) {
          return (A) clazz.getAnnotation(annotationType);
        }
      }
    }
    return null;
  }
}
